package com.hua.zhifubao;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alipay.sdk.app.AuthTask;
import com.alipay.sdk.app.EnvUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    public static final String APPID = "9021000141649348";
    //私钥
    public static final String RSA2_PRIVATE = "MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQC4Stt4NtX1bazrqznm/NCLDHh54X+upPfWpZkRDwe/1BNAqR094NaD+2OfrEZZ5sWU5K/NMM2DSqnJ7qSIUpKira+0gh/0Iadtj+MyH17V5zBCsHHnPD1h+fuJfY8z6zUEEkPbObzWydephd1KP+6vGuHhOe9EIrSJWGD6VddfKEFPtFFFdNEUVHXi+cURPojRwUhz5ZZkrH5jJWL0n9COd3eYQ3hict/7gWWvLtRGcvsI/WkHMHnzWLO/X/LTSN2q+UIIb2zT1udN+o4Rl2GHUqITWR310WULNNFcUN9ilPa8F84XxjmTFn9B28Oj7we65cvFNTPllazaSlAVUXM7AgMBAAECggEBAJkzIV69tv9fPPBsVqX+ZB4zL7OiEVJNMPnuj1/u7rI4yghmjThzP+BXimmh/JmfjYBI2xvoBe+ukZacG4p3mg5B9F5KDeYUU+pwQp3YFi4bEbNemlFvcleQR9nz1qeCT4Ai7uW/CYxEHlN+RIcEvpYx8rQYRR6O+yVxoqx3z6H5m3CprcGbS3Qe5JBdD55WiWaMmuWS3pWFBpmtetNv6qGAvxgocXcLaAQ/6XDhOBeMFq68fFKdZqd4OYMWI2STzz9sR0oX8OO/0unFqGWXViclqPnJ/Z8nFTLFWliyJyGIMafl26b0Rx2HM3692HUu1J7ahurblE0iu/6tdPcNnwECgYEA4XJy9joXdV3d2RwEgzsKbJjtMfvzZudRLP8z4os9Ub2ydyZaP9rBjd7pY4+LmPFIzVILUcLz7R84CKyW/aG+Lv1B3o7fxESkWnfP/EyEJgx8zfEHLobJW/2X3F4qIkv4xbmcOPn1kk0Lkw8uRWSrfopTJsmVIwk46aIS9fvweZkCgYEA0USbzxMkKEo5v4P2PBG4Ul4qngOlx4vs5DrfvLReEZEvm/U4pQW3CgMk/QHj2POOjpNLNOSh7d4h3dgkuflFRRsN5K94rQP0RFCGW7w0aIWbL1WU0FcGliBcAw+vf5Ijzr6OgxZfSSKjnZpsue27CBxG+5GuDc//yvT1LF3In/MCgYEAtknzOLLKCwVl/0nPQEj56ctRZywgqCD7mxWS32fUogZvijYBnUYFYPBP6EfGCVl3k0T2kBrBXwbyKNlckSI6BAaVPx5pQmp6NghQrOE1rQpF08NDGlSz9eS76Nxe1zJ0qXOmJM+/x5byd+s7b7Kxk/TGvUMbiqPHV+nLyQf4bmkCgYEA0S6JqLZzgCqiCwR30JfN7dffNdBjmFIQXBtVpqWNGnZMZtL66koKK2H1SUroXOco6u/lT1vzWXif1cfG/ndjfK6MdrnIIPpA40Cy7WP15z0WYHxlotQ66zoxf4XgYd7NGE522iY03UBY2KOSZ1BxkqvhcHqwx3HROSkfIlgkwW0CgYEAxzGTFYu/jm1EfzBY75VyO1aaOqKTgOFUXvG1sh9P3zuOZqKZA2p5j27Stnd0auUaLb8/LhRp8OheJsKMso8JDXw8DaYvWqZLdT+sHthT57Wu3MfmKZZ9IGd9rxT3Q/JsuyyGlnV9ZRdUrPpwlwDNNI48uwRxuGTEqGHZmLTaGZU=";
    private RecyclerView rc_view;
    //创建一个List  原始数据源
    private List<RcBean> list = new ArrayList<>();
    //渲染用的列表
    private List<RcBean> newList = new ArrayList<>();
    //实例化Adapter
    RcAdapter adapter = new RcAdapter(newList);
    public static final int SDK_PAY_FLAG = 0x200;
    private static final String TAG = "====日志====";

    private final Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SDK_PAY_FLAG:

                    Map<String, String> resultMap = (Map<String, String>) (Map<String, String>)msg.obj;

                    if (resultMap.get("resultStatus").equals("9000")) {
                        Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                    }

                    if (resultMap.get("resultStatus").equals("6001")) {
                        Toast.makeText(MainActivity.this, resultMap.get("memo").toString(), Toast.LENGTH_SHORT).show();
                    }

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //进入沙盒环境
        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        setContentView(R.layout.activity_main);

        //初始化控件
        initView();
        //初始化加数据
        setData();
        //点击方法
        setOnClickListener();

    }

    private void setData() {

        /*这里用两个List另有他用，有点经验的应该知道，新手朋友造写吧*/
        list.clear();
        newList.clear();
        RcBean rcBean1 = new RcBean("斐济杯", new BigDecimal(1000.00));
        RcBean rcBean2 = new RcBean("刀魔", new BigDecimal(999.00));
        list.add(rcBean1);
        list.add(rcBean2);

        newList.addAll(list);



    }

    private void setOnClickListener() {

        adapter.setOnClickListener(position -> {

            Log.d(TAG, "newList.get(position).getMoney(): " + newList.get(position).getMoney());

            // TODO: 2024/10/23 这里写支付代码模块
            Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(APPID, String.valueOf(newList.get(position).getMoney()), true);
            String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
            String privateKey = RSA2_PRIVATE;
            String sign = OrderInfoUtil2_0.getSign(params, privateKey, true);

            final String orderInfo = orderParam + "&" + sign;

            Log.d(TAG, "orderInfo: " + orderInfo);

            Runnable payRunnable = new Runnable() {
                @Override
                public void run() {
                    AuthTask alipay = new AuthTask(MainActivity.this);
                    Map<String, String> result = alipay.authV2(orderInfo, true);
                    Message msg = new Message();
                    msg.what = SDK_PAY_FLAG;
                    msg.obj = result;
                    mHandler.sendMessage(msg);

                }
            };

            Thread payThread = new Thread(payRunnable);
            payThread.start();

        });


    }

    private void initView() {
        rc_view = findViewById(R.id.rc_view);
        rc_view.setLayoutManager(new LinearLayoutManager(this));
        rc_view.setAdapter(adapter);



    }
}